package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysCmpMapper;
import com.ruoyi.system.domain.SysCmp;
import com.ruoyi.system.service.ISysCmpService;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-20
 */
@Service
public class SysCmpServiceImpl implements ISysCmpService 
{
    @Resource
    private SysCmpMapper sysCmpMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysCmp selectSysCmpById(String id)
    {
        return sysCmpMapper.selectSysCmpById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param sysCmp 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysCmp> selectSysCmpList(SysCmp sysCmp)
    {
        return sysCmpMapper.selectSysCmpList(sysCmp);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param sysCmp 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysCmp(SysCmp sysCmp)
    {
        sysCmp.setCreateTime(DateUtils.getNowDate());
        return sysCmpMapper.insertSysCmp(sysCmp);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param sysCmp 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysCmp(SysCmp sysCmp)
    {
        sysCmp.setUpdateTime(DateUtils.getNowDate());
        return sysCmpMapper.updateSysCmp(sysCmp);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysCmpByIds(String[] ids)
    {
        return sysCmpMapper.deleteSysCmpByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysCmpById(String id)
    {
        return sysCmpMapper.deleteSysCmpById(id);
    }

    @Override
    public String importCmp(List<SysCmp> cmpList, Boolean isUpdateSupport, Long userId) {
        if (StringUtils.isNull(cmpList) || cmpList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        for (SysCmp sysCmp : cmpList) {
            System.out.println("=====================================");
            //验证该条数据是否存在
            SysCmp sysCmpSel = new SysCmp();
            sysCmpSel.setEnName(sysCmp.getEnName());
            List<SysCmp> sysCmps = sysCmpMapper.selectSysCmpList(sysCmpSel);
            if (sysCmps != null && sysCmps.size() > 0) {
                if (isUpdateSupport) {
                    sysCmp.setUpdateUserId(userId);
                    sysCmp.setUpdateTime(new Date());
                    SysCmp cmp = sysCmps.get(0);
                    BeanUtils.copyProperties(sysCmp, cmp);
                    sysCmpMapper.updateSysCmp(cmp);
                }
            } else {
                //新增
                sysCmp.setCreateUserId(userId);
                sysCmp.setCreateTime(new Date());
                sysCmpMapper.insertSysCmp(sysCmp);
            }
        }
        return "success";
    }
}
